/**
 * filename: UserInfoController.java
 * author: luruifeng
 * date: 2016年3月2日
 * http: <a href='http://www.anzeinfo.com'>www.anzeinfo.com</a>
 * Copyright (c) 2016 Anze, Inc. All rights reserved.
 */
package com.anze.abd.ui.userinfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.anze.abd.constant.MyConstant;
import com.anze.abd.persistence.domain.UserInfo;
import com.anze.abd.service.userinfo.UserInfoService;
import com.anze.base.constant.BaseConstant;
import com.anze.base.persistence.domain.Role;
import com.anze.base.persistence.domain.User;
import com.anze.base.service.role.RoleService;
import com.anze.base.service.user.UserService;
import com.anze.base.ui.commons.BaseController;
import com.anze.base.utils.PageNavInfoUtils;
import com.anze.base.utils.StringUtils;
import com.anze.base.utils.WebUtils;

/**
 * 用户信息
 * @author luruifeng 2016年3月2日
 * @version 2.4
 * @since 2.4 2016年3月2日
 */
@Controller
@RequestMapping("userinfo")
public class UserInfoController extends BaseController {
	
	@Resource(name = "userInfoService")
	private UserInfoService userInfoService;
	
	@Resource(name = "userService")
	private UserService userService;
	
	@Resource(name = "roleService")
	private RoleService roleService;
	
	@RequestMapping()
	public ModelAndView list() {
		PageNavInfoUtils.setPageNavInfo();
		String userName = WebUtils.getSession().getAttribute(BaseConstant.SESSION_USER_NAME).toString();
		ModelAndView mv = new ModelAndView("/userinfo/user_info_list");
		mv.addObject("userName", userName);
		return mv;
	}
	
	@RequestMapping(value="add", method=RequestMethod.POST)
	public @ResponseBody String save(HttpServletRequest request){
		String userName = request.getParameter("userName").trim();
		String name = request.getParameter("name").trim();
		String age = request.getParameter("age");
		String email = request.getParameter("email");
		
		userService.saveUser(userName);
		User user = userService.getUserByName(userName);
		
		UserInfo info = new UserInfo();
		if(!StringUtils.isEmpty(age)){
//			info.setAge(age);
		}
		if(!StringUtils.isEmpty(email)){
			info.setEmail(email);
		}
		info.setName(name);
		info.setUser(user);
		info.setUserid(user.getId());
		
		userInfoService.saveUserInfo(info);
		
		return "true";
	}
	
	/**
	 * 更新用户信息
	 * @param request
	 * @param id
	 * @return
	 * @throws Exception
	 * @author luruifeng 2016年3月2日
	 * @version 2.4
	 * @since 2.4 2016年3月2日
	 */
	@RequestMapping(value="{id}", method=RequestMethod.PUT)
	public @ResponseBody String update(HttpServletRequest request,@PathVariable Long id) throws Exception {
		String userInfoName = request.getParameter("realName");
		String age = request.getParameter("age");
		String email = request.getParameter("email");
		HttpSession session = request.getSession();
		if(StringUtils.isNotEmpty(userInfoName)){
			if(null != session){
				UserInfo userInfo = (UserInfo) session.getAttribute(MyConstant.SESSION_USERINFO);
				User user = userService.getUser(id);
				if(null != userInfo){
					age = StringUtils.isEmpty(age) ? "0" : age;
					userInfoService.updateUserInfo(userInfo.getName(),userInfoName,age,email,userInfoService.getByUser(user).getId());
					return "true";
				}
			}
		} 
		return "false";
	}
	
	/**
	 * 获取个人信息
	 * @param userInfoId
	 * @return
	 * @throws Exception
	 * @author luruifeng 2016年3月2日
	 * @version 2.4
	 * @since 2.4 2016年3月2日
	 */
	@RequestMapping(value = "/get/{userInfoId}", method = RequestMethod.GET)
	public @ResponseBody UserInfo getUserInfo(@PathVariable Long userInfoId) throws Exception {
		UserInfo userInfo = userInfoService.getUserInfo(userInfoId);
		UserInfo target = new UserInfo();
		target.setName(userInfo.getName());
//		target.setAge(userInfo.getAge());
		target.setEmail(userInfo.getEmail());
//		return converStringCodeToISO8859(gson.toJson(target));
		return target;
	}
	
	/**
	 * 获取用户角色
	 * @param id
	 * @return
	 * @throws Exception
	 * @author luruifeng 2016年3月2日
	 * @version 2.4
	 * @since 2.4 2016年3月2日
	 */
	@RequestMapping(value="{id}/role")
	public @ResponseBody Map<String,List<Role>> userRole(@PathVariable Long id) throws Exception {
		Map<String,List<Role>> map = new HashMap<String, List<Role>>();
		List<Role> roles = roleService.findEnabled();
		
		String userName = WebUtils.getSession().getAttribute(BaseConstant.SESSION_USER_NAME).toString();
		if (StringUtils.isBlank(userName) || !userName.equals("superadmin")) {
			roles = roleService.findEnabledNotSuperAdmin();
		}
		
		if (null == roles) {
			throw new RuntimeException("未获取到角色");
		}
		
		User user = userService.getUser(userInfoService.getUserInfo(id).getUserid());
		if (null == user) {
			throw new RuntimeException("未找到[id="+ id +"]的用户]");
		}
		List<Role> userRole = roleService.findUserRoles(user);
		map.put("userRole", userRole);
		map.put("roles", roles);
		return map;
	}

}
